package cn.hsp.demo

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.IconToggleButton
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import cn.hsp.demo.ui.theme.HspDemoTheme

/**
 * 厦门大学计算机专业 | 前华为工程师
 * 专注《零基础学编程系列》https://cxyxy.blog.csdn.net/article/details/121134634
 * 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
 * 公众号：花生皮编程
 */

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TorchUtils.init(this)
        setContent {
            HspDemoTheme {
                Surface(
                    modifier = Modifier.fillMaxSize().background(Color.Black),
                    color = Color.Black
                ) {
                    HspPage()
                }
            }
        }
    }

}

@Composable
fun HspPage(
) {
    var isTurnedOn by remember { mutableStateOf(false) }

    IconToggleButton(
        checked = isTurnedOn,
        onCheckedChange = {
            if (isTurnedOn) {
                TorchUtils.close()
            } else {
                TorchUtils.open()
            }
            isTurnedOn = it
        },
    ) {
        Image(painterResource(getImageRes(isTurnedOn)), "")
    }
}

@Composable
private fun getImageRes(isTurnedOn: Boolean): Int {
    return if (isTurnedOn) {
        R.drawable.turn_off
    } else {
        R.drawable.turn_on
    }
}